package com.znsd.zl.app.reservation.controller;


import com.znsd.zl.app.openfeigin.reservation.SysUserFeign;
import com.znsd.zl.app.reservation.entity.ReservationEntity;
import com.znsd.zl.app.reservation.service.IReservationService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import com.znsd.zl.vo.req.SendSmsReqVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName ReservationController
 * @Description 预约管理控制层
 * @author jbb
 * @email jbb@163.com
 * @date 2020-08-21 21:13:34
 * @Version v1.0
 */
@RestController
@RequestMapping("user/reservation")
public class ReservationController extends BaseController<IReservationService, ReservationEntity,Integer> {

    @Resource
    private SysUserFeign feign;

    /**
     * 分页查询
     * @param pageNum  当前请求页码
     * @param pageSize 请求的记录数
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public R queryPage(@RequestParam Map<String, Object> params,
                       @PathVariable(name = "pageNum") Integer pageNum,
                       @PathVariable(name = "pageSize") Integer pageSize, HttpServletRequest request) {

        PageUtils page = null;
        try{
            page = service.queryPage(params, pageNum, pageSize);
        }catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器异常，请稍后重试");
        }
        return R.ok().put("page", page);
    }

    @GetMapping("/pending")
    public R getPending(){
        LinkedHashMap user =(LinkedHashMap) feign.info().get("user");
        return R.ok().put("count",service.getPending(user.get("userId").toString()));
    }

    @PutMapping("/adopt")
    public R adopt(@RequestParam Map<String,Object> params){
        Object status = params.get("status");
        LinkedHashMap user =(LinkedHashMap) feign.info().get("user");
        String condition = "1";
        /**
         *预约成功发送消息
         */
        if (status!=null && condition.equals(status)) {
            SendSmsReqVo smsReqVo = new SendSmsReqVo();
            smsReqVo.setPhone(params.remove("phone").toString());
            smsReqVo.setContent(params.get("name")!=null?params.remove("name").toString():"用户");
            smsReqVo.setTemplateCode("SMS_201451003");
            try{
                R r = feign.sendReservedSms(smsReqVo);
                Integer code = r.get("code") !=null?Integer.parseInt(r.get("code").toString()):0;
                Integer sendStatusCondition = 0;
                if (!(code.equals(sendStatusCondition))) {
                    return R.error("系统繁忙，请联系管理");
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("系统繁忙，请联系管理");
            }

        }
        params.put("emp_id", user.get("userId"));
        service.selectUpdate(params,params.remove("id"));

        return R.ok();
    }

    @GetMapping("/isReservation")
    public R isReservation(@RequestParam Integer userId,@RequestParam Integer houseId){
        return R.ok().put("reservation",service.isReservation(userId,houseId));
    }

}

